# Maintainer: Renato Silva <br.renatosilva@gmail.com>

_realname='nsis'
pkgbase="mingw-w64-${_realname}-svn"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-svn"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=r6749
pkgrel=1
url='https://nsis.sourceforge.io/'
pkgdesc='Windows installer development tool (mingw-w64)'
license=(ZLIB)
arch=(any)
depends=(${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-libwinpthread)
makedepends=(${MINGW_PACKAGE_PREFIX}-gcc
             'scons>=2.3.4-3'
             rsync
             svn)
source=(${_realname}.svn::svn+'https://svn.code.sf.net/p/nsis/code/NSIS/trunk'
        001-fhs-directory-structure.patch
        002-compile-fixes.patch
        003-remove-redundant-architecture-suffix.patch)
sha256sums=('SKIP'
            '70e21760b037b03c6b73b3babb418c386ee2e2c18f4c99a5c3b9040c1a18b20f'
            '92dd16e882aa6270b2d6a75392ebff4514332af7abd5e02da3c7874fb5953aa3'
            '0a7a2f7c671d7776def53e2e0974b803096409fc886e78393761092556084d9e')

# Circumvent problem where makepkg will add the exe extension to some files
# when compressing the 64-bit package
options=('!strip')

pkgver() {
    cd "${srcdir}/${_realname}.svn"
    printf "r%s" "$(svnversion | tr -d 'A-z')"
}

consolidate() {
    msg2 'Converting to unix line breaks'
    find -name 'SConstruct' | xargs dos2unix --quiet
    find -name 'SConscript' | xargs dos2unix --quiet
    find -name '*.[ch]'     | xargs dos2unix --quiet
    find -name '*.cpp'      | xargs dos2unix --quiet
    find -name '*.py'       | xargs dos2unix --quiet
}

prepare() {
    cd "${srcdir}/${_realname}.svn"
    if [[ -n $(svn diff SConstruct) ]]; then
        msg2 'Skipping already applied patches'
        return 0
    fi
    consolidate
    patch -p1 < "${startdir}"/001-fhs-directory-structure.patch
    patch -p1 < "${startdir}"/002-compile-fixes.patch
    patch -p1 < "${startdir}"/003-remove-redundant-architecture-suffix.patch
}

_build() {
    local target_arch="${CARCH/i686/x86}"
    target_arch="${target_arch/x86_64/amd64}"

    local revision="${pkgver#r}"
    msg2 "Revision defined as ${revision}"

    cd "${srcdir}/${_realname}.build.${CARCH}"
    scons \
        TARGET_ARCH="${target_arch}" \
        PREFIX="${MINGW_PREFIX}" \
        VERSION="${pkgver}" \
        VER_REVISION="${revision}" \
        UNICODE='yes' \
        NSIS_MAX_STRLEN='8192' \
        NSIS_CONFIG_CONST_DATA_PATH='no' \
        SKIPUTILS='NSIS Menu,SubStart' \
        PREFIX_DEST="${PREFIX_DEST}" \
        ${target}
}

build() {
    msg2 'Synchronizing build directory'
    rsync --recursive --times "${srcdir}/${_realname}.svn"/* "${srcdir}/${_realname}.build.${CARCH}"
    _build
}

package() {
    PREFIX_DEST="${pkgdir}" target='install' _build
    install -Dm644 "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
    rm "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING"
}
